<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('参股基金')" prop="dealId">
            <el-select v-model="form.dealId" :disabled="form.id === undefined ? false : true" filterable clearable :placeholder="$t('请选择')">
              <el-option
                v-for="item in dealNameOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出资时间')" prop="contributionTime">
            <DatePicker v-model="form.contributionTime" :placeholder="$t('出资时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('划款金额') + $t('金额单位')" prop="transferAmount">
            <Money v-model="form.transferAmount" unit="w" :placeholder="$t('划款金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('持股比例(%)')" prop="contributionShare">
            <Money v-model="form.contributionShare" :placeholder="$t('持股比例(%)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('更新时间')" prop="changeTime">
            <DatePicker v-model="form.changeTime" :placeholder="$t('出资时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('累计划款金额') + $t('金额单位')" prop="accumulatedPlanPayment">
            <Money v-model="form.accumulatedPlanPayment" disabled unit="w" :placeholder="$t('累计划款金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="bf:investGroupInfo" />
    </div>
  </div>
</template>

<script>
    import { saveBfInvestGroupInfo, getBfInvestGroupInfo } from '@/api/bf/investGroup/bfInvestGroupInfo'
    import { lisDealName } from '@/api/bf/investGroup/bfInvestGroup'
    export default {
        data() {
            return {
                dealNameOptions: [],
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  dealId: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  transferAmount: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                // 查询公司指标参数
                queryParams: {
                  indirectDealId: this.companyId || this.$route.query.companyId,
                  source: 'BF_PORTFOLIO'
                }
            }
        },
        created() {
            // 获取所有关联项目
            lisDealName(this.queryParams).then(response => {
              this.dealNameOptions = response.rows
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    getBfInvestGroupInfo(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    companyId: this.$route.query.companyId,
                    dealId: this.$route.query.dealId,
                    contributionTime: undefined,
                    transferAmount: undefined,
                    contributionShare: undefined,
                    changeTime: this.getUpQuarterDay(),
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveBfInvestGroupInfo(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          /**
           * 根据当前时间计算上季度最后一天是哪一年那一天
           */
          getUpQuarterDay() {
            // 获取当前时间
            var date = new Date()
            // 获取当前月份
            var month = date.getMonth()
            // 获取当前年份
            var year = date.getFullYear()
            // 判断当前月份属于第几季度
            if (month >= 0 && month <= 2) {
              return new Date(year - 1, 12, 0)
            } else if (month >= 3 && month <= 5) {
              return new Date(year, 3, 0)
            } else if (month >= 6 && month <= 8) {
              return new Date(year, 6, 0)
            } else if (month >= 9 && month <= 11) {
              return new Date(year, 9, 0)
            }
          }
        }

    }
</script>
